﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TraceInspector.Application;
using System.Collections.ObjectModel;
using TraceInspector.Model;
using GalaSoft.MvvmLight.Command;

namespace TraceInspector.ViewModel
{
    public class InspectionListViewModel : ToolViewModel
    {
        IUIDataProvider _dataProvider;

        public InspectionListViewModel(IUIDataProvider dataProvider,IToolManager toolManager)
            : base(toolManager)
        {
            _dataProvider = dataProvider;
            Title = "Выбор обследования";
        }

        #region Properties

        private ObservableCollection<Inspection> _inspections;
        public ObservableCollection<Inspection> InspectionList
        {
            get
            {
                if (IsServerSource)
                {
                    return _inspections ?? (_inspections = _dataProvider.GetInspections());
                }
                // вернуть локальные обследования
                return null;
            }
        }

        public bool IsSetCurrentInspection
        {
            get { return (_currentInspection != null ? true : false); }
        }

        private Inspection _currentInspection;
        /// <summary>
        /// Указывает на текущее обследование, просматриваемое пользователем
        /// </summary>
        public Inspection CurrentInspection
        {
            get { return _currentInspection; }
            set
            {
                _currentInspection = value;
                RaisePropertyChanged("CurrentInspection");
                RaisePropertyChanged("IsSetCurrentInspection");
            }
        }


        private bool _IsServerSource = true;

        /// <summary>
        /// Истина  - список обследований берется с сервера, ложь - список берется с локального диска
        /// </summary>
        public bool IsServerSource
        {
            get { return _IsServerSource; }
            set
            {
                _IsServerSource = value;
                RaisePropertyChanged("IsServerSource");
                // вызываем чтобы обновились привязки данных
                RaisePropertyChanged("InspectionList");
            }
        }

        
        private Model.Inspection _selectedInspection;

        public Model.Inspection SelectedInspection
        {
            get { return _selectedInspection; }
            set { _selectedInspection = value; RaisePropertyChanged("SelectedInspection"); }
        }
        

        #endregion

        #region Methods

        public override string ToString()
        {
            return "Список доступных для редактирования обследований";
        }

        void SelectInspection()
        {
            SelectedInspection = CurrentInspection;  
        }

        #endregion

        #region Commands

        private RelayCommand _selectInspectionCommand;

        public RelayCommand SelectInspectionCommand
        {
            get { return _selectInspectionCommand ?? (_selectInspectionCommand = new RelayCommand(SelectInspection, () => IsSetCurrentInspection)); }
        }

        #endregion
    }
}
